//
//  MovieDetailViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/24.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieCommentTableViewCell.h"
#import "AllMovieCommentViewController.h"
#import "CommitStarsView.h"
#import "PhotosViewController.h"
#import "MoviePhotoCollectionViewCell.h"
#import "SharePopView.h"
static NSString *cellIdentify = @"MovieCommentTableViewCell";

static NSString * CellIdentifer = @"MoviePhotoCollectionViewCell";


@interface MovieDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,MWPhotoBrowserDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UIView *movieInfoView;
@property (strong, nonatomic) IBOutlet UIImageView *movieImage;
@property (strong, nonatomic) IBOutlet UILabel *movieName;
@property (strong, nonatomic) IBOutlet UILabel *movieScore;
@property (strong, nonatomic) IBOutlet UILabel *movieInfo;
@property (strong, nonatomic) IBOutlet UILabel *movieType;
@property (strong, nonatomic) IBOutlet UILabel *movieTime;
@property (strong, nonatomic) IBOutlet UILabel *movieStartDate;
@property (strong, nonatomic) IBOutlet UIButton *wantWatchButton;
@property (strong, nonatomic) IBOutlet UIButton *commitScoreButton;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic,strong)  NSMutableArray  *photos;
@property(nonatomic,strong)NSMutableArray * imageArray;
@property(nonatomic,strong)MWPhotoBrowser * browser;


@property (strong, nonatomic) IBOutlet UILabel *directorName;//导演
@property (strong, nonatomic) IBOutlet UILabel *startActor;
@property (strong, nonatomic) IBOutlet UILabel *movieHistory;


@property (strong, nonatomic) IBOutlet UIButton *commitComentButton;//发影评按钮




@property(nonatomic,strong)NSMutableArray * commitArray;
@property(nonatomic,strong)NSMutableArray * heightArray;

@end

@implementation MovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _commitArray = [NSMutableArray array];
    _heightArray = [NSMutableArray array];
    _photos = [NSMutableArray array];
    _imageArray = [@[@"photos1",@"photos1",@"photos1",@"photos1",@"photos1"]mutableCopy];
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.movieInfoView];
    
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 801), YES);
    [_movieHistory sizeToFit];
    
    _wantWatchButton.layer.borderWidth = 1.5;
    _wantWatchButton.layer.borderColor =[UIColor whiteColor].CGColor;
    _wantWatchButton.layer.cornerRadius = 3*VerticalRatio();
    

    _commitScoreButton.layer.borderWidth = 1.5;
    _commitScoreButton.layer.borderColor =[UIColor whiteColor].CGColor;
    _commitScoreButton.layer.cornerRadius = 3*VerticalRatio();
    
    _commitComentButton.layer.borderWidth = 1;
    _commitComentButton.layer.borderColor = [UIColor colorWithRed:0.980 green:0.624 blue:0.000 alpha:1.000].CGColor;
    _commitComentButton.layer.cornerRadius = 3*VerticalRatio();
    
    
    CommitStarsView * stars = [[CommitStarsView alloc]initWithFrame:flexibleFrame(CGRectMake(105, 37, 80, 15), NO) Stars:3.5];
    [_movieInfoView addSubview:stars];
    
//      
//    for (int i = 0; i<_commitArray.count; i++) {
//        NSArray * imageArray = _commitArray[i][@"imageArray"];
//        NSArray * commitArray = _commitArray[i][@"commentArray"];
//        NSInteger height = 0;
//        if (imageArray.count ==0 && commitArray.count == 1) {
//            height = 120;
//        }else if (imageArray.count >0 && commitArray.count == 1){
//            height = 170;
//        }else{
//            for (int j = 0; j<commitArray.count; j++) {
//                NSString * comment = commitArray[j];
//                int lines = 0;
//                CGSize cSize ;
//                if (j==0) {
//                    cSize = [comment sizeWithFont:[UIFont systemFontOfSize:14*VerticalTexeRatio()]];
//                }else{
//                    cSize = [comment sizeWithFont:[UIFont systemFontOfSize:11*VerticalTexeRatio()]];
//                }
//                
//                if (cSize.width<290*VerticalRatio()) {
//                    lines=1;
//                }else{
//                    lines = cSize.width/290*VerticalRatio()+1;
//                }
//                height+= lines*(cSize.height);
//            }
//            
//            if (imageArray.count ==0 && commitArray.count >1){
//                height += 120;
//            }else if (imageArray.count >0 && commitArray.count >1){
//                height += 170;
//            }
//        }
//        [_heightArray addObject:@(height)];
//    }
    
    _browser = [[MWPhotoBrowser alloc] init];
    
    // Set options
    _browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
    _browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
    _browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
    _browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
    _browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
    _browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
    _browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
    
    // Optionally set the current visible photo before displaying
    // Manipulate
    [_browser showNextPhotoAnimated:YES];
    [_browser showPreviousPhotoAnimated:YES];
    
    //注册
    [self.tableView registerNib:[UINib nibWithNibName:cellIdentify bundle:nil] forCellReuseIdentifier:cellIdentify];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 8*VerticalRatio();
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.userInteractionEnabled=YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:CellIdentifer bundle:nil] forCellWithReuseIdentifier:CellIdentifer];

}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}
#pragma mark == 分享
- (IBAction)shareHandel:(id)sender {
    SharePopView * popView = [[SharePopView alloc]initWithTitles:@[@"微信好友",@"朋友圈",@"新浪微博",@"QQ好友"] complect:^(NSInteger index) {
        switch (index) {
            case 0:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
                
            }
                break;
            case 1:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 2:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToSina] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            case 3:
            {
                [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        //            NSLog(@"分享成功！");
                    }
                }];
            }
                break;
            default:
                break;
        }

    }];
    [popView show];

}
#pragma mark == 发布影评
- (IBAction)commitComment:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"CommitMovieComment"];
    [self.navigationController   pushViewController:page animated:YES];
}

#pragma mark == 查看全部评论
- (IBAction)showAllComment:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    AllMovieCommentViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AllMovieComment"];
    page.type = 1;
    [self.navigationController   pushViewController:page animated:YES];
}


#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify ];
    if (!cell) {
        cell = [[MovieCommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
//    for (UIView *view in cell.subviews) {
//        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
//            [view removeFromSuperview];
//        }
//    }
    
    CommitStarsView * stars = [[CommitStarsView alloc]initWithFrame:flexibleFrame(CGRectMake(13, 45, 80, 15), NO) Stars:3.5];
    [cell addSubview:stars];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSInteger height= [_heightArray[indexPath.row] integerValue];
    return 107*VerticalRatio();
    
}


#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArray.count;
    
    
    
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(75*VerticalRatio(), 50*VerticalRatio());
    
    
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,8*VerticalRatio(), 0, 0);
    
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MoviePhotoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifer forIndexPath:indexPath];
    
    cell.phoyoImage.image = [UIImage imageNamed:_imageArray[indexPath.row]];
    //                if ([dic[@"mgooCategoryId"] isEqualToString:[NSString stringWithFormat:@"%@",self.goodsClassifArray[indexPath.row][@"mgcaId"]]]) {
    //                    cell.goodsInfo.text = dic[@"mgooName"];
    //                    NSString * image = dic[@"appAdertImagUri"];
    //                    if (image.length>0) {
    //                        [cell.goodsImage sd_setImageWithURL:[NSURL URLWithString:image]];
    //                    }else{
    //                        cell.goodsImage.image =[UIImage imageNamed:@"cloth1"];
    //                    }
    //                }
    
    
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    [_photos removeAllObjects];
//    for (int i = 0; i<_imageArray.count; i++) {
//        MWPhoto * photo = [[MWPhoto alloc]initWithImage:[UIImage imageNamed:@"photos1"]];
//        [_photos addObject:photo];
//    }
//    _browser.delegate = self;
//    [_browser setCurrentPhotoIndex:indexPath.row];
//    _browser.titleStr = @"剧照";
//    // Present
//    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_browser];
//    [self presentViewController:nc animated:YES completion:nil];
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    PhotosViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Photos"];
    page.imagesArray = @[];
    page.photosType = @"2";
    [self.navigationController   pushViewController:page animated:YES];
}


- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    return nil;
}

- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index{
    NSString * str = [NSString stringWithFormat:@"%ld/%ld",index+1,_photos.count];
    return str;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
